package vn.candyapp.qlct;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import vn.candyapp.qlct.fragment.DSGiaoDichFragment;
import vn.candyapp.qlct.item.KieuTKGiaoDich;
import vn.candyapp.qlct.utils.DateTimeUtil;
import vn.candyapp.widget.QuickAction;
import vn.candyapp.widget.TitlePageIndicator;
import vn.candyapp.widget.TitlePageIndicator.IndicatorStyle;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;

public class DSGiaoDichActivity extends ActionBarActivity {

	
	private KieuTKGiaoDich mType;
	private ViewPager mPager;
	private GDFragmentPagerAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		setContentView(R.layout.title_pager_layout);
		mPager = (ViewPager) findViewById(R.id.pager);
		
		Bundle args = getIntent().getExtras().getBundle("DATA");
		mType = (KieuTKGiaoDich) args.getSerializable("TYPE");
		
		mAdapter = new GDFragmentPagerAdapter(getSupportFragmentManager());
		mPager.setAdapter(mAdapter);
		mPager.setCurrentItem(mAdapter.gotoToday());
		TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
		indicator.setViewPager(mPager);
		indicator.setFooterIndicatorStyle(IndicatorStyle.Triangle);
	}

	@Override
	public boolean onSupportNavigateUp() {
		finish();
		return super.onSupportNavigateUp();
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.transaction_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case R.id.menu_add_transaction:
				Intent intent = new Intent(this, AddTransactionActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(intent);
			
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}


	public class GDFragmentPagerAdapter extends FragmentPagerAdapter {

		private List<Date> mData = new ArrayList<Date>();
		private List<String> mTitles = new ArrayList<String>();
		private Calendar cal = Calendar.getInstance();
		
		public GDFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
			prepareData();
		}
		
		public void prepareData() {
			int calKey = Calendar.DATE;
			int delta = 100;
			SimpleDateFormat sdf = DateTimeUtil.FORMAT_DATE;
			
			switch (mType) {
				case HOMNAY:
					calKey = Calendar.DATE;
					delta = 100;
					break;
				case THANGNAY:
					calKey = Calendar.MONTH;
					sdf = DateTimeUtil.FORMAT_MONTH;
					delta = 24;
					break;
				case TUANNAY:
					calKey = Calendar.WEEK_OF_YEAR;
					delta = 96;
					break;
				case NAMNAY:
					calKey = Calendar.YEAR;
					sdf = DateTimeUtil.FORMAT_YEAR;
					delta = 5;
					break;
			}
			cal.add(calKey, -delta);
			for (int i=1;i<=delta;i++) {
				cal.add(calKey, 1);
				Date d = cal.getTime();
				mData.add(d);
				if (mType == KieuTKGiaoDich.TUANNAY) {
					Date sow = DateTimeUtil.getFirstDayOfWeek(d);
					Date eow = DateTimeUtil.getLastDayOfWeek(d);
					mTitles.add(String.format("%s-%s", DateTimeUtil.FORMAT_DATE.format(sow), DateTimeUtil.FORMAT_DATE.format(eow)));
				} else {
					mTitles.add(sdf.format(d));
				}
			}
		}
		
		public int gotoToday() {
			return mData.size() - 1;
		}

		@Override
		public Fragment getItem(int position) {
			return DSGiaoDichFragment.newInstance(mData.get(position), mType);
		}

		@Override
		public int getCount() {
			return mData.size();
		}
		
		public CharSequence getPageTitle(int position) {
			return mTitles.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			super.destroyItem(container, position, object);
		}
		
	}

}
